<?php

namespace app\v1\controller;

use app\v1\model\Answer;
use app\v1\model\CountryIntro;
use app\v1\model\QuestionOptionResult;
use think\Controller;
use think\Request;

class User extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save(Request $request)
    {
        $user_validate = validate('v1/user');
        $post = $request->post();
        if (empty($post)) return json(['msg' => '数据不能为空']);
        if (!$user_validate->check($post)) {
            return json(['msg' => $user_validate->getError()]);
        }
        // 解析成unionId
        $res = (new \app\v1\model\User)->getOpenId($post['code']);
        if (!empty($res['errcode'])) return json(['msg' => $res['errmsg']]);
        // 查询是否能在数据库查询到该openid的用户
        $user = \app\v1\model\User::where('open_id', $res['openid'])->find();
        if ($user) return json($user->toArray());;
        $post['open_id'] = $res['openid'];
        return json(\app\v1\model\User::create($post));
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    /**
     * 获取用户最后一次测试结果
     * @param int $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLastResultByUser(int $id)
    {
        // 获取用户最后一次answer 的订单号
        $last_order_no = Answer::where('user_id', $id)
            ->order('create_time', 'DESC')
            ->value('order_no');
        if (empty($last_order_no)) return json(['msg' => '未查询到该用户的测试信息', 'code' => 2001]);
        // 获取同一订单的数据
        $last_user_answer = Answer::where(['user_id' => $id, 'order_no' => $last_order_no])->select();
        // 需要匹配的题
        $arr_question = [2 => 0, 6 => 0, 7 => 0];
        foreach ($last_user_answer as $item) {
            if (isset($arr_question[$item['question_id']])) {
                $arr_question[$item['question_id']] = $item['option_id'];
            }
        }
        $option_id_concat = implode(',', $arr_question);
        // 根据逗号连接的选项ID 查询符合要求的移民方法
        $result = QuestionOptionResult::where('option_id_concat', $option_id_concat)->find();
        if (!empty($result)) {
            $country_intro = CountryIntro::whereIn('id', $result['country_intro_id_concat'])->select();
            return json(['msg' => '', 'code' => 200, 'data' => $country_intro]);
        } else {
            return json(['msg' => '未查询到符合要求的移民方式', 'code' => 2002]);
        }
    }
}
